/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "gmock/gmock-actions.h"
#include "mockcpp/mockcpp.hpp"
#include "manager/CloudServiceManager.h"
#include "common/CommonStruct.h"
#include "common/constant/PluginServiceCommon.h"
#include "CommonJobFactory.h"
#include "obs_backup/ObjectStorageBackupJob.h"

using namespace std;
using namespace Module;

namespace {
constexpr auto MP_SUCCESS = 0;
constexpr auto INTERNAL_ERROR = 200;
}  // namespace

class CommonJobFactoryTest : public testing::Test {
public:
    void SetUp() {};
    void TearDown()
    {
        GlobalMockObject::verify();
    };
};

/*
 * 用例名称：获取备份任务类型
 * 前置条件：无
 * check点：返回成功
 */
TEST_F(CommonJobFactoryTest, GetBackupAppType)
{
    auto commonJobFactoryPtr = OBSPlugin::CommonJobFactory::GetInstance();

    auto data = std::make_shared<AppProtect::BackupJob>();
    data->jobParam.backupType = AppProtect::BackupJobType::FULL_BACKUP;
    std::shared_ptr<JobCommonInfo> jobCommonInfo = std::make_shared<JobCommonInfo>(data);
    EXPECT_EQ(
        commonJobFactoryPtr->GetAppType(jobCommonInfo, JobType::BACKUP),
        static_cast<uint64_t>(OBSPlugin::AppType::APPTYPE_OBJECT_STORAGE));

    data->jobParam.backupType = AppProtect::BackupJobType::SNAPSHOT;
    jobCommonInfo = std::make_shared<JobCommonInfo>(data);
    EXPECT_EQ(
        commonJobFactoryPtr->GetAppType(jobCommonInfo, JobType::BACKUP),
        static_cast<uint64_t>(OBSPlugin::AppType::APPTYPE_NONE));
}

/*
 * 用例名称：获取恢复任务类型
 * 前置条件：无
 * check点：返回成功
 */
TEST_F(CommonJobFactoryTest, GetRestoreAppType)
{
    auto commonJobFactoryPtr = OBSPlugin::CommonJobFactory::GetInstance();

    auto data = std::make_shared<AppProtect::RestoreJob>();
    std::shared_ptr<JobCommonInfo> jobCommonInfo = std::make_shared<JobCommonInfo>(data);
    EXPECT_EQ(
        commonJobFactoryPtr->GetAppType(jobCommonInfo, JobType::RESTORE),
        static_cast<uint64_t>(OBSPlugin::AppType::APPTYPE_OBJECT_STORAGE));
}

/*
 * 用例名称：获取索引任务类型
 * 前置条件：无
 * check点：返回成功
 */
TEST_F(CommonJobFactoryTest, GetIndexAppType)
{
    auto commonJobFactoryPtr = OBSPlugin::CommonJobFactory::GetInstance();

    auto data = std::make_shared<AppProtect::BuildIndexJob>();
    std::shared_ptr<JobCommonInfo> jobCommonInfo = std::make_shared<JobCommonInfo>(data);
    EXPECT_EQ(
        commonJobFactoryPtr->GetAppType(jobCommonInfo, JobType::INDEX),
        static_cast<uint64_t>(OBSPlugin::AppType::APPTYPE_INDEX));
}

/*
 * 用例名称：创建任务
 * 前置条件：无
 * check点：返回任务指针
 */
TEST_F(CommonJobFactoryTest, CreateJob)
{
    auto commonJobFactoryPtr = OBSPlugin::CommonJobFactory::GetInstance();

    auto data = std::make_shared<AppProtect::BackupJob>();
    data->jobParam.backupType = AppProtect::BackupJobType::FULL_BACKUP;
    std::shared_ptr<JobCommonInfo> jobCommonInfo = std::make_shared<JobCommonInfo>(data);
    EXPECT_NE(commonJobFactoryPtr->CreateJob(jobCommonInfo, JobType::BACKUP), nullptr);
    EXPECT_EQ(commonJobFactoryPtr->CreateJob(jobCommonInfo, JobType::UNDEFINED_JOB_TYPE), nullptr);

    EXPECT_NE(commonJobFactoryPtr->CreateFactoryJob<OBSPlugin::ObjectStorageBackupJob>(jobCommonInfo), nullptr);
}